<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('系统字典列表')" />
    <style>
        .node {
            cursor: pointer;
        }

        .node circle {
            fill: #fff;
            stroke: steelblue;
            stroke-width: 1.5px;
        }

        .node text {
            font: 10px sans-serif;
        }

        .link {
            fill: none;
            stroke: #ccc;
            stroke-width: 1.5px;
        }
        .tree{
            width: 800px;
            height: 400px;
            margin: 0 auto;
            background: #E0E0E0;
        }
        .tree svg{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">

         <div class="row">
             <div class="col-sm-9">
                 <div class="ibox">
                     <div class="ibox-title">
                         <h5>向下依赖</h5>
                     </div>
                     <div class="ibox-content">
                         <div class="tree" id="treeDown"></div>
                     </div>
                 </div>
                 <div class="ibox">
                     <div class="ibox-title">
                         <h5>向上依赖</h5>
                     </div>
                     <div class="ibox-content">
                         <div class="tree" id="treeUp"></div>
                     </div>
                 </div>
             </div>
             <div class="col-sm-3">
                 <div class="ibox">
                     <div class="ibox-title">
                         <h5>节点详情</h5>
                     </div>
                     <div class="ibox-content" style="height: 800px">

                     </div>
                 </div>
             </div>
         </div>
    </div>
    <th:block th:include="include :: footer" />
     <script th:src="@{/js/d3.min.js}"></script>
    <script th:inline="javascript">
        var json = [
            {
                "name": "门诊人次",
                "parent": "null",
                "children": [
                    {
                        "name": "门急诊人次",
                        "parent": "张三",
                        "children": [
                            {
                                "name": "王五",
                                "parent": "李四",
                                "children": [
                                    {"name": "程六","parent": "王五" },
                                    {"name": "王八","parent": "王五" },
                                    {"name": "王七","parent": "王五" },
                                    {
                                        "name": "王九",
                                        "parent": "王五",
                                        "children":[
                                            {"name": "王十","parent": "王五" },
                                            {"name": "王十一","parent": "王五" },
                                            {"name": "王十二","parent": "王五" },
                                            {"name": "王十三","parent": "王五" },
                                            {"name": "王十四","parent": "王五" },
                                            {
                                                "name": "程五",
                                                "parent": "王五",
                                                "children":[
                                                    {"name": "程六","parent": "程五" },
                                                    {"name": "程七","parent": "程五" },
                                                    {"name": "程八","parent": "程五" },
                                                    {"name": "小张","parent": "小红" },
                                                    {"name": "小王","parent": "小红" },
                                                    {"name": "小程","parent": "小红" },
                                                    {"name": "小陈","parent": "小红" },
                                                    {"name": "小明","parent": "小红" },
                                                    {"name": "小张","parent": "小红" },
                                                    {"name": "小王","parent": "小红" },
                                                    {"name": "小程","parent": "小红" },
                                                    {"name": "小陈","parent": "小红" },
                                                    {"name": "小明","parent": "小红" },
                                                    {"name": "小张","parent": "小红" },
                                                    {"name": "小王","parent": "小红" },
                                                    {"name": "小程","parent": "小红" },
                                                    {"name": "小陈","parent": "小红" },
                                                    {"name": "小明","parent": "小红" },
                                                    {"name": "小张","parent": "小红" },
                                                    {"name": "小王","parent": "小红" },
                                                    {"name": "小程","parent": "小红" },
                                                    {"name": "小陈","parent": "小红" },
                                                    {"name": "程九","parent": "程五" },
                                                    {
                                                        "name": "程十",
                                                        "parent": "程五",
                                                        "children":[
                                                            {"name": "程十一","parent": "程十" },
                                                            {"name": "程十二","parent": "程十" },
                                                            {"name": "程十三","parent": "程十" },
                                                            {"name": "小张","parent": "小红" },
                                                            {"name": "小王","parent": "小红" },
                                                            {"name": "小程","parent": "小红" },
                                                            {"name": "小陈","parent": "小红" },
                                                            {"name": "小明","parent": "小红" },
                                                            {"name": "小张","parent": "小红" },
                                                            {"name": "小王","parent": "小红" },
                                                            {"name": "小程","parent": "小红" },
                                                            {"name": "小陈","parent": "小红" },
                                                            {"name": "小明","parent": "小红" },
                                                            {"name": "小张","parent": "小红" },
                                                            {"name": "小王","parent": "小红" },
                                                            {"name": "小程","parent": "小红" },
                                                            {"name": "小陈","parent": "小红" },
                                                            {"name": "小明","parent": "小红" },
                                                            {"name": "小张","parent": "小红" },
                                                            {"name": "小王","parent": "小红" },
                                                            {"name": "小程","parent": "小红" },
                                                            {"name": "小陈","parent": "小红" },
                                                        ]
                                                    },
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                "name": "挂号（DW）",
                                "parent": "门急诊人次",
                                "children":[
                                    {"name": "HIS挂号","parent": "马一" },
                                ]
                            }
                        ]
                    }
                ]
            },
        ];
        var margin = [10, 0, 0, 120],
            width = document.getElementById("treeDown").offsetWidth,
            height = document.getElementById("treeUp").offsetHeight;

        var i = 0,
            duration = 750,
            root;

        var treeDown = d3.layout.tree().size([height, width]);
        var treeUp = d3.layout.tree().size([height, width]);

        var diagonal = d3.svg.diagonal()
            .projection(function(d) { return [d.y, d.x]; });
        var zoomDown = d3.behavior.zoom().scaleExtent([0.1, 100]).on("zoom", zoomedDown);//添加放大缩小事件
        var zoomUp = d3.behavior.zoom().scaleExtent([0.1, 100]).on("zoom", zoomedUp);//添加放大缩小事件
        var svgDown = d3.select("body").select("#treeDown").append("svg")
            .call(zoomDown)//给svg绑定zoom事件
            .append("g")
            .call(zoomDown)//给g绑定zoom事件
            .append("g")
            .attr("transform", "translate(" + margin[3] + "," + margin[0] + ")");

        var svgUp = d3.select("body").select("#treeUp").append("svg")
            .call(zoomUp)//给svg绑定zoom事件
            .append("g")
            .call(zoomUp)//给g绑定zoom事件
            .append("g")
            .attr("transform", "translate(" + margin[3] + "," + margin[0] + ")");


        root = json[0];
        root.x0 = height / 2;
        root.y0 = 0;

        function collapse(d) {
            if (d.children) {
                d._children = d.children;
                d._children.forEach(collapse);
                d.children = null;
            }
        }

        root.children.forEach(collapse);
        update(root,svgDown,treeDown);
        update(root,svgUp,treeUp);

        function zoomedDown() {
            svgDown.attr("transform",
                "translate(" + zoomDown.translate() + ")" +
                "scale(" + zoomDown.scale() + ")"
            );
        }

        function zoomedUp() {
            svgUp.attr("transform",
                "translate(" + zoomUp.translate() + ")" +
                "scale(" + zoomUp.scale() + ")"
            );
        }

        function update(source,target,tree) {
            debugger;
            // Compute the new tree layout.
            var nodes = tree.nodes(root).reverse(),
                links = tree.links(nodes);

            // Normalize for fixed-depth.
            nodes.forEach(function(d) { d.y = d.depth * 180; });

            // Update the nodes…
            var node = target.selectAll("g.node")
                .data(nodes, function(d) { return d.id || (d.id = ++i); });

            // Enter any new nodes at the parent's previous position.
            var nodeEnter = node.enter().append("g")
                .attr("class", "node")
                .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
                .on("click", click);

            nodeEnter.append("circle")
                .attr("r", 1e-6)
                .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

            nodeEnter.append("text")
                .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
                .attr("dy", ".35em")
                .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
                .text(function(d) { return d.name; })
                .style("fill-opacity", 1e-6);

            var nodeUpdate = node.transition()
                .duration(duration)
                .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

            nodeUpdate.select("circle")
                .attr("r", 4.5)
                .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

            nodeUpdate.select("text")
                .style("fill-opacity", 1);

            var nodeExit = node.exit().transition()
                .duration(duration)
                .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
                .remove();

            nodeExit.select("circle")
                .attr("r", 1e-6);

            nodeExit.select("text")
                .style("fill-opacity", 1e-6);

            var link = target.selectAll("path.link")
                .data(links, function(d) { return d.target.id; });

            link.enter().insert("path", "g")
                .attr("class", "link")
                .attr("d", function(d) {
                    var o = {x: source.x0, y: source.y0};
                    return diagonal({source: o, target: o});
                });

            link.transition()
                .duration(duration)
                .attr("d", diagonal);

            link.exit().transition()
                .duration(duration)
                .attr("d", function(d) {
                    var o = {x: source.x, y: source.y};
                    return diagonal({source: o, target: o});
                })
                .remove();

            nodes.forEach(function(d) {
                d.x0 = d.x;
                d.y0 = d.y;
            });
        }

        function click(d) {
            if (d.children) {
                d._children = d.children;
                d.children = null;
            } else {
                d.children = d._children;
                d._children = null;
            }
            update(d);
        }
    </script>
</body>
</html>